#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include "adminwindow.h"
#include <QFile>
#include "staffwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("超市管理系统");
    ui->timelabel->setText(QTime::currentTime().toString("hh:mm:ss"));
    TimeThread* th1 = new TimeThread; //思路，将UI控件传入到子线程，在子线程去使用控件
    //槽函数如果是重载函数时，那么，在建立信号和槽的时候，要显示指定的重载操作
   // connect(th1,&TimeThread::sendMsg,this,QOverload<>::of(&MainWindow::updateTime));
    connect(th1,&TimeThread::sendData,this,QOverload<>::of(&MainWindow::updateTime));
    th1->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_pushButton_clicked()
{
    QString identity = ui->comboBox->currentText();
    static int count = 0;
    static int count1 = 0;
    if(identity == "管理员")
    {
        if(ui->namelineEdit->text() == "Lone" && ui->passwordlineEdit->text() == "20000807")
        {
            QMessageBox::information(this,"登录提示","登录成功");
            AdminWindow *aw = new AdminWindow;
            this->close();
            aw->show();
        }

        else if(ui->namelineEdit->text() == "" || ui->passwordlineEdit->text() == "")
        {
            QMessageBox::information(this,"登录提示","请规范输入信息");
            return;
        }

        else
        {

            QMessageBox::information(this,"登录提示","账号or密码错误");
            count++;
            if(count == 3)
            {
                QMessageBox::information(this,"Warning","系统即将退出");
                exit(1);
            }

        }

    }



    else if(identity == "员工")
    {
        //1.读取文件中的用户信息，并跟用户输入的信息进行比较
        QFile file("./user.txt");
        if(file.open(QIODevice::ReadOnly))
        {

            //2.循环读取文件中的数据，并跟用户输入的用户名和密码进行比较，如果相等，则不要再读取文件数据
            while (!file.atEnd())
            {
                //3.第一次读用户名
                QString name =  file.readLine(); //用户名\r\n
                name = name.remove("\r\n"); //用户名
                //4.第二次读密码
                QString password = file.readLine(); //密码\r\n
                password = password.remove("\r\n");

                //5.比较是否相等
                if(name == ui->namelineEdit->text() && password == ui->passwordlineEdit->text())
                {
                    //登录提示
                    QMessageBox::information(this,"登录提示","登录成功");
                    staffWindow *sw = new staffWindow;
                    this->close();
                    sw->show();
                    break;
                }

                else if(ui->namelineEdit->text() == "" || ui->passwordlineEdit->text() == "")
                {
                    QMessageBox::information(this,"登录提示","请规范输入信息");
                    return;
                }

                else
                {

                    QMessageBox::information(this,"登录提示","账号or密码错误");
                    count1++;
                    if(count1 == 3)
                    {
                        QMessageBox::information(this,"Warning","系统即将退出");
                        exit(1);
                    }

                }

            }


        }

        file.close();
    }
}

void MainWindow::on_checkBox_stateChanged(int arg1)
{
    if(arg1 == 2)
    {
        ui->passwordlineEdit->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->passwordlineEdit->setEchoMode(QLineEdit::Password);
    }
}

void MainWindow::updateTime()
{
    ui->timelabel->setText(QTime::currentTime().toString("hh:mm:ss"));
}

void MainWindow::updateTime(QString time)
{
    ui->timelabel->setText(time);
}
